package calendar;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author angelika
 */
public class Days extends JPanel {
    Calendar parent;
    GregorianCalendar gCalendar;
    Integer currentDay;
    
    public Days(Calendar cal) {
        parent = cal;
        gCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
        setOpaque(false);
        setLayout(new GridLayout(0, 7, 10, 10));
        newDate(gCalendar.get(GregorianCalendar.YEAR), 
                gCalendar.get(GregorianCalendar.MONTH),
                gCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }
    
    public Date getDate() {
        return new Date(currentDay, gCalendar.get(GregorianCalendar.MONTH), gCalendar.get(GregorianCalendar.YEAR));
    }
    
    public void newDate(Integer year, Integer month) {
        newDate(year, month, currentDay);
    }
    
    public void newDate(Integer year, Integer month, Integer day) {
        currentDay = day;
        gCalendar.set(year, month, 1);
        Integer d = gCalendar.get(GregorianCalendar.DAY_OF_WEEK);
        // На западе первым днём недели считается воскресение
        d = d - 1;
        if (d == 0) {
            d = 7;
        }
        
        Integer daysInMonth = gCalendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
        
        removeAll();
        String[] daysOfWeek = { "пн", "вт", "ср", "чт", "пт", "сб", "вс" };
        for (String dayOfWeek : daysOfWeek) {
            JLabel dayLabel = new JLabel(dayOfWeek);
            dayLabel.setHorizontalAlignment(JLabel.CENTER);
            if (dayOfWeek == "сб" || dayOfWeek == "вс") {
                dayLabel.setForeground(Color.red);
            }
            add(dayLabel);
        }
        
        for (Integer i = 1; i < d; i++) {
            add(new JLabel(" "));
        }
        for (Integer i = 1; i <= daysInMonth; i++) {
            JLabel dayLabel = new JLabel(i.toString());
            dayLabel.setHorizontalAlignment(JLabel.CENTER);
            if ((d + i) % 7 == 0 || (d + i - 1) % 7 == 0) {
                dayLabel.setForeground(Color.lightGray);
            }
            if (i == day) {
                dayLabel.setOpaque(true);
                dayLabel.setBackground(Color.darkGray);
                dayLabel.setForeground(Color.white);
            }
            dayLabel.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent me) {
                    newDate(gCalendar.get(GregorianCalendar.YEAR), 
                            gCalendar.get(GregorianCalendar.MONTH),
                            Integer.parseInt(dayLabel.getText()));
                }
                public void mousePressed(MouseEvent me) { }
                public void mouseReleased(MouseEvent me) { }
                public void mouseEntered(MouseEvent me) { }
                public void mouseExited(MouseEvent me) { }
            });
            add(dayLabel);
        }
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        revalidate();
        repaint();
        
        parent.newDate(year, month, day);
    }
}
